package com.heima.article.listener;

import com.heima.common.constants.article.HotArticleConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class HotArticleScoreListener {
    @Autowired
    StringRedisTemplate redisTemplate;

    @RabbitListener(queuesToDeclare = @Queue(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_QUEUE))
    public void articleBehaviorHandler(String jsonStr){
        try {
            ListOperations<String, String> opsForList = redisTemplate.opsForList();
            log.info("监听到消息，{}",jsonStr);
            Long aLong = opsForList.rightPush(HotArticleConstants.HOT_ARTICLE_SCORE_BEHAVIOR_LIST, jsonStr);
            log.info("存入redis成功，{}",aLong);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("articleBehaviorHandler 监听消息队列出错");
        }
    }
}
